package com.android.fragmentdemo.defineviews
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

class Dy01ItemDecoration(mRightSpace: Float) : RecyclerView.ItemDecoration() {
    private val rightSpace = mRightSpace
    private var setFirstItemSpace = false

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        val position = parent.getChildAdapterPosition(view)
        val itemCount = parent.adapter?.itemCount ?: 0
        outRect.right = if (position == itemCount - 1) 0 else rightSpace.toInt()
        outRect.left = if (this.setFirstItemSpace && position == 0) rightSpace.toInt() else 0
    }

    fun setFirstItemSpace(setFirstItemSpace: Boolean) {
        this.setFirstItemSpace = setFirstItemSpace
    }
}